// Copyright 2019 The Go Authors. All rights reserved.
// Use of this session code is governed by a BSD-style
// license that can be found in the LICENSE file.

package lsp

import (
	"context"
	"mi/internal/lsp/session"
	"mi/internal/xpath"

	"mi/internal/lsp/protocol"
)

func (s *Server) hover(ctx context.Context, params *protocol.HoverParams) (*protocol.Hover, error) {
	data, err := s.session.GetHoverInfo(ctx, xpath.URI(params.TextDocument.URI), toUTF16Position(params.Position))
	if err != nil {
		return nil, err
	}
	if data == nil {
		return nil, nil
	}

	return toProtocolHover(data), nil
}

func toProtocolMarkupContent(content session.MarkupContent) protocol.MarkupContent {
	return protocol.MarkupContent{
		Kind:  protocol.MarkupKind(content.Kind),
		Value: content.Value,
	}
}

func toProtocolHover(info *session.HoverInfo) *protocol.Hover{
	return &protocol.Hover{
		Contents: toProtocolMarkupContent(info.Contents),
		Range:    toProtocolRange(info.Range),
	}
}